# coding:utf-8
from odoo import models, fields, api, _
from odoo.exceptions import Warning


class PaulSaleOrderReasonWizard(models.TransientModel):
    _name = 'sale.order.reason.wizard'

    approve_reason_ids = fields.Many2many('sale.order.reason', string='Approve Reason')

    @api.multi
    def save_reason_action(self):
        self.ensure_one()
        if len(self.approve_reason_ids) == 0:
            raise Warning(_('Approve reasons is required.'))
        if self._context.get('default_model') == 'sale.order' and self._context.get(
                'sale_order_ids') and self._context.get('mark_so_as_sent'):
            order = self.env['sale.order'].browse([self._context['sale_order_ids']])
            if order.state == 'draft':
                order.state = 'sent'
                order.reject_approve = False

        vals = {}
        if self._context.get('sale_order_ids'):
            vals['sale_order_ids'] = [(4, [self._context.get('sale_order_ids')])]
        if vals:
            self.approve_reason_ids.write(vals)
        return {'type': 'ir.actions.act_window_close'}